// RedJey Graphics User Interface Library for J2ME Platform.
// Copyright (C) 2008 Yevgen Krapiva
//
// This library is free software; you can redistribute
// it and/or modify it under the terms of the
// GNU Lesser General Public License as published
// by the Free Software Foundation; either version 3.0
// of the License, or any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//

package com.incrediblesoft.redjey;

/**
 * @author Yevgen Krapiva<br>
 *         Homepage: http://code.google.com/p/redjey/<br>
 *         Feedback: ykrapiva@gmail.com<br>
 *         Copyright (C) 2008 Yevgen Krapiva<br>
 */

import com.incrediblesoft.redjey.event.MouseEvent;

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.Displayable;
import java.util.Hashtable;

public class TextField extends TextComponent implements CommandListener
{
    protected int maxSize, constraint;
    protected String title;
    private String oldtext;

    public TextField(String title, String text, int maxSize, int constraint)
    {
        this(title, text, maxSize, constraint, null);
    }
    
    public TextField(String title, String text, int maxSize, int constraint, String styleSetName)
    {
        super(text, styleSetName);
        this.title = title;
        this.maxSize = maxSize;
        this.constraint = constraint;
    }

    public String getName()
    {
        return "TextField";
    }

    protected Dimension calcPreferredSize(int paddingHorizontal, int paddingVertical, CustomFont font, Hashtable attributes)
    {
        return new Dimension(currentStyle.font.stringWidth(text), font.getHeight());
    }

    protected void calcContentSize()
    {
        contentSize.width = currentStyle.font.stringWidth(text);
        contentSize.height = currentStyle.font.getHeight();
    }

    public void processMouseEvent(MouseEvent event)
    {
        switch(event.getID())
        {
            case MouseEvent.MOUSE_CLICKED:
                oldtext = this.getText();
                UIManager.box.setConstraints(constraint);
                UIManager.box.setMaxSize(maxSize);
                UIManager.box.setString(text);
                UIManager.box.setTitle(title);
                UIManager.box.setCommandListener(this);
                UIManager.getDisplay().setCurrent(UIManager.box);
                break;
        }

        super.processMouseEvent(event);
    }

    public void commandAction(Command command, Displayable displayable)
    {
        if (command.getCommandType() == Command.OK)
        {
            UIManager.getDisplay().setCurrent(UIManager.getCanvas());
            UIManager.repaint();
            setText(UIManager.box.getString());
        }
        else if (command.getCommandType() == Command.CANCEL)
        {
            UIManager.getDisplay().setCurrent(UIManager.getCanvas());
            UIManager.repaint();
        }
    }

    public void setText(String text)
    {
        super.setText(text);
        if (parent != null) parent.invalidate();
    }

    protected void validateComponent()
    {

    }
    
    public void paintComponent(Graphics g)
    {
        currentStyle.font.drawString(g, text, 0, 0, Graphics.LEFT | Graphics.TOP);
    }
}
